package com.example.rbacmanage.controller;

import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.Area;
import com.example.rbacmanage.service.AreaService;
import com.example.rbacmanage.vo.AreaTreeResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    @GetMapping("/tree")
    public Result tree(Area area){
        List<AreaTreeResp> tree = areaService.tree(area);
        return Result.success(tree);
    }

    @GetMapping("/{id}")
    public Result info(@PathVariable(value = "id") Long id){
        return Result.success(areaService.getById(id));
    }

    @PreAuthorize("hasAnyAuthority('system:area:add')")
    @PostMapping("/add")
    public Result add(@RequestBody Area area){
        areaService.save(area);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('system:area:update')")
    @PutMapping("/update")
    public Result update(@RequestBody Area area){
        areaService.update(area);
        return Result.success();
    }


    @PreAuthorize("hasAnyAuthority('system:area:remove')")
    @DeleteMapping("/remove")
    public Result remove(@RequestBody Long[] ids){
        areaService.remove(ids);
        return Result.success();
    }
}
